//
//  PhotosCollection.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/30.
//

#import "PhotosCollection.h"
#import "PhotoCollectionViewCell.h"

@implementation PhotosCollection

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        self.backgroundColor = [UIColor whiteColor];
        self.dataSource = self;
        self.delegate = self;
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        [self registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:@"PhotoCollectionViewCell"];
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCollectionViewCell" forIndexPath:indexPath];
    id img = self.dataArr[indexPath.item];
    if ([img isKindOfClass:[UIImage class]]) {
        cell.image = (UIImage *)img;
    }else {
        cell.imageUrl = (NSString *)img;
    }
    @weakify(self)
    cell.deleteVideoBlock = ^(NSString * _Nonnull imageUrl){
     @strongify(self)
        if (self.deleteVideoBlock) {
            self.deleteVideoBlock(imageUrl);
        }
    };
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.didSelectedCellSubject sendNext:indexPath];
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (RACSubject *)didSelectedCellSubject {
    if (!_didSelectedCellSubject) {
        _didSelectedCellSubject = [RACSubject subject];
    }
    return _didSelectedCellSubject;
}


@end
